package com.demo.demo.stream;

import com.jayway.jsonpath.internal.function.numeric.Max;
import net.bytebuddy.dynamic.loading.InjectionClassLoader;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class StreamDemo {
    public static void main(String[] args) {
        List<String> strings = Arrays.asList("abc", "bc", "efg", "abcd", "jkl","jkl","efg");
        System.out.println(strings);
        /**
         * stream()是创建stream
         * filter( s ->s == "")转化成stream流，里面的条件是筛选
         * count计算个数
         */
        long num=strings.stream().filter( s ->s == "").count();
        System.out.println(num);
        /**
         * 生成一个无限长度的Stream，其元素的生成是通过给定的Supplier
         */
        Stream.generate(new Supplier<Double>() {
            @Override
            public Double get() {
                return Math.random();
            }
        });
        Stream.generate(() -> Math.random());
        Stream.generate(Math::random);

        /**
         * 遍历
         */
        strings.stream().iterator().forEachRemaining(ele-> System.out.print(ele+","));
        System.out.println();
        Stream.iterate(1,item->item+1).limit(20).forEach(System.out::print);
        System.out.println();

        /**
         * 去重distinct()方法
         */
        strings=strings.stream().distinct().collect(Collectors.toList());
        strings.stream().iterator().forEachRemaining(System.out::println);

        List<Student> students = new ArrayList<>();
        students.add(new Student(1,"s"));
        students.add(new Student(1,"g"));
        students.add(new Student(2,"z"));

        /**
         * skip()跳过 limit()限制个数，从第一个开始截取不算第一个
         */
        strings.stream().skip(1).limit(2).collect(Collectors.toList()).forEach(els-> System.out.println(els));

        /**
         * reduce()累加
         */
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 2, 2, 2, 2);
        Integer reduce = integers.stream().reduce(0, (integer1, integer2) -> integer1 + integer2);
        System.out.println(reduce);
        Integer reduce1 = integers.stream().reduce(0,Integer::sum);
        System.out.println(reduce1);
        /**
         * 获取最大值
         */
        Optional<Integer> integer = integers.stream().reduce(Integer::max);
        System.out.println(integer);

        Optional<Integer> integer1 =integers.stream().max(Integer::compareTo);
        System.out.println(integer1);

        /**
         * 获取最小值
         */
        Optional<Integer> integer2 = integers.stream().reduce(Integer::min);
        System.out.println(integer2);

        Optional<Integer> integer3 =integers.stream().min(Integer::compareTo);
        System.out.println(integer3);

        integers=integers.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(integers);

        /**
         *anyMatch()检查是否至少匹配一个元素匹配一个则返回false
         */
        boolean a = strings.stream().anyMatch(s -> s.contains("a"));
        System.out.println(a);

        /**
         *allMatch()检查是否全部满足满足返回false
         */
        boolean b = strings.stream().allMatch(s -> s.length() > 0);
        System.out.println(b);

        /**
         *noneMatch()检查是否全部都不满足条件，如果不满足返回true
         */
        boolean b1 = strings.stream().noneMatch(s -> s.length() >0);
        System.out.println(b1);

        /**
         * findAny()查找任意一个元素，默认拿第一个
         */
        Optional<String> any = strings.stream().findAny();
        System.out.println(any.get());

        /**
         * 并行流会拿到的不是第一个
         */
        Optional<String> s = strings.parallelStream().findAny();
        System.out.println(s.get());

        /**
         * findFirst()查找第一个元素
         */
        Optional<String> first = strings.parallelStream().findFirst();
        System.out.println(first.get());
    }

}
